package com.wq.ui.coms;

import java.awt.Point;
import java.util.ArrayList;

import javax.swing.Icon;
import javax.swing.JLabel;

/**
 * 移动控制类
 * @author wq
 * 2020/12/19 - 17:43
 *
 */
public class BasicMoveLabel extends JLabel 
{
	private float speed;
	private boolean moveOver;
	private ArrayList<Point> allPosition = new ArrayList<Point>();
	private Point next ;
	
	private float minDistance;
	
	private Thread thread;
	public BasicMoveLabel(Icon icon)
	{
		super(icon);
	}
	
	public void init(float speed,float minDistance)
	{
		this.speed = speed;
		this.minDistance = minDistance;
		
	}
	
	/**
	 * 按顺序要到达的点
	 * @param point
	 */
	public void setNextPosition(Point... points)
	{
		for(int c = 0 ; c < points.length ;c++)
		{
			allPosition.add(points[c]);
		}
		
		if(points.length > 0 && next == null)
		{
			next = allPosition.get(0);
		}
		if(thread == null || !thread.isAlive())
		{
			thread = new Thread(new MoveThread());
			thread.start();
		}
	}
	public boolean moveIsOver()
	{
		return !thread.isAlive();
	}
	
	class MoveThread implements Runnable
	{
		private boolean hasDrawed = false;
		private int DEFAULT_FPS = 60;
		private int gameState = 0;
		private long nowFPS;
		private int ms = 0;
		private boolean xOver,yOver;
		private boolean allOver;
		
		public void run()
		{
			// 计算出指定FPS数值中，每帧需要多少时间  
			long fpsTime = (long) ((Double.valueOf(1000) / Double.valueOf(DEFAULT_FPS)) * 1000000);  
			// 绘制图像前的时间戳  
			long now = 0;  
			// 每次绘制图像耗时（毫秒）  
			long total = 0;  
			while(true) {  
				now = System.nanoTime();  
				// 绘制图像  
//		          case 0://主菜单状态
				if(next == null)
				{
//					System.out.println(next);
					ms++;
					if(ms > 100)
					{
						return;
					}else
					{
						continue;
					}
				}
				
				Point p = BasicMoveLabel.this.getLocation();
				int xLen = next.x - p.x;
				int yLen = next.y - p.y;
				if(Math.abs(xLen)>minDistance)
				{
					BasicMoveLabel.this.setLocation(BasicMoveLabel.this.getX() + (int)(xLen*speed/Math.abs(xLen)), BasicMoveLabel.this.getY());
				}else
				{
					BasicMoveLabel.this.setLocation(next.x, BasicMoveLabel.this.getY());
					xOver = true;
				}
				
				if(Math.abs(yLen)>minDistance)
				{
					BasicMoveLabel.this.setLocation(BasicMoveLabel.this.getX()  , BasicMoveLabel.this.getY()+(int)(yLen*speed/Math.abs(yLen)));
				}else
				{
					BasicMoveLabel.this.setLocation(BasicMoveLabel.this.getX(), next.y);
					yOver = true;
				}
//				BasicMenuPane.this.setLocation(x, y);
				
				if(xOver&&yOver)
				{
					allPosition.remove(0);
					if(allPosition.size() > 0)
					{
						next = allPosition.get(0);
					}else
					{
						next = null;
						allOver = true;
					}
					xOver = false;
					yOver = false;
				}
				
				try{  
					// 除去绘制之后还需要休眠的时间  
					total = System.nanoTime() - now;  
				
					if(total > fpsTime){  
						nowFPS = 1000000*1000/total;
						// 如果本次绘制时间超过每帧需要绘制的时间，则直接继续绘制  
						continue;  
					}  
					Thread.sleep((fpsTime - (System.nanoTime()- now)) / 1000000);  
					total = System.nanoTime() - now;  
					nowFPS = 1000000*1000/total;
				} catch (InterruptedException e) {  
					e.printStackTrace();  
				}  
				while((System.nanoTime()- now) < fpsTime){  
					// 使用循环，精确控制每帧绘制时长  
					System.nanoTime();  
				}  
			}  
		}
	}
	
}
